import { IConfig, IPlugin } from 'umi-types';
import defaultSettings from './defaultSettings'; // https://umijs.org/config/

import slash from 'slash2';
import webpackPlugin from './plugin.config';
const { pwa, primaryColor } = defaultSettings;

// preview.pro.ant.design only do not use in your production ;
// preview.pro.ant.design 专用环境变量，请不要在你的项目中使用它。
const { ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION } = process.env;
const isAntDesignProPreview = ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site';
const plugins: IPlugin[] = [
  [
    'umi-plugin-react',
    {
      antd: true,
      dva: {
        hmr: true,
      },
      locale: {
        // default false
        enable: true,
        // default zh-CN
        default: 'zh-CN',
        // default true, when it is true, will use `navigator.language` overwrite default
        baseNavigator: true,
      },
      dynamicImport: {
        loadingComponent: './components/PageLoading/index',
        webpackChunkName: true,
        level: 3,
      },
      pwa: pwa
        ? {
          workboxPluginMode: 'InjectManifest',
          workboxOptions: {
            importWorkboxFrom: 'local',
          },
        }
        : false,
      // default close dll, because issue https://github.com/ant-design/ant-design-pro/issues/4665
      // dll features https://webpack.js.org/plugins/dll-plugin/
      // dll: {
      //   include: ['dva', 'dva/router', 'dva/saga', 'dva/fetch'],
      //   exclude: ['@babel/runtime', 'netlify-lambda'],
      // },
    },
  ],
  [
    'umi-plugin-pro-block',
    {
      moveMock: false,
      moveService: false,
      modifyRequest: true,
      autoAddMenu: true,
    },
  ],
]; // 针对 preview.pro.ant.design 的 GA 统计代码

if (isAntDesignProPreview) {
  // plugins.push([
  //   'umi-plugin-ga',
  //   {
  //     code: 'UA-72788897-6',
  //   },
  // ]);
  plugins.push([
    'umi-plugin-pro',
    {
      serverUrl: 'https://ant-design-pro.netlify.com',
    },
  ]);
}

export default {
  plugins,
  block: {
    // 国内用户可以使用码云
    // defaultGitUrl: 'https://gitee.com/ant-design/pro-blocks',
    defaultGitUrl: 'https://github.com/ant-design/pro-blocks',
  },
  hash: true,
  targets: {
    ie: 11,
  },
  devtool: isAntDesignProPreview ? 'source-map' : false,
  // umi routes: https://umijs.org/zh/guide/router.html
  routes: [
    {
      path: '/user',
      component: '../layouts/UserLayout',
      routes: [
        {
          name: 'login',
          path: '/user/login',
          component: './user/login',
        },
      ],
    },
    {
      path: '/',
      component: '../layouts/SecurityLayout',
      routes: [
        {
          path: '/',
          component: '../layouts/BasicLayout',
          authority: ['admin', 'user'],
          routes: [
            {
              path: '/',
              redirect: '/welcome',
            },
            {
              path: '/workbench',
              name: 'workbench',
              icon: 'smile',
              component: './workbench',
            },
            {
              path: '/customer',
              name: 'customer',
              icon: 'smile',
              routes: [
                {
                  path: '/customer/potential',
                  name: 'potential',
                  icon: 'smile',
                  component: './customer/potential',
                },
                {
                  path: '/customer/baseinfo',
                  name: 'baseinfo',
                  icon: 'smile',
                  component: './customer/baseinfo',
                },
              ],
            },
            {
              path: '/supplier',
              name: 'supplier',
              icon: 'smile',
              routes: [
                {
                  path: '/supplier/baseinfo',
                  name: 'baseinfo',
                  icon: 'smile',
                  component: './supplier/baseinfo',
                },
                {
                  path: '/supplier/order',
                  name: 'order',
                  icon: 'smile',
                  component: './supplier/order',
                },
              ],
            },
            {
              path: '/item',
              name: 'item',
              icon: 'smile',
              routes: [
                // {
                //   path: '/item/category',
                //   name: 'category',
                //   icon: 'smile',
                //   component: './item/category',
                // },
                // {
                //   path: '/item/publishProduct',
                //   name: 'publishProduct',
                //   icon: 'smile',
                //   component: './item/publishProduct',
                // },
                // {
                //   path: '/item/productList',
                //   name: 'productList',
                //   icon: 'smile',
                //   component: './item/productList',
                // },
                // {
                //   path: '/item/publishItem',
                //   name: 'publishItem',
                //   icon: 'smile',
                //   component: './item/publishItem',
                // },
                {
                  path: '/item/itemList/sfLocalFee',
                  name: 'sfLocalFee',
                  icon: 'smile',
                  component: './item/itemList/sfLocalFee',
                },
                {
                  path: '/item/itemList',
                  name: 'itemList',
                  icon: 'smile',
                  routes: [
                    {
                      name: 'declareFee',
                      icon: 'smile',
                      path: '/item/itemList/declareFee',
                      component: './item/itemList/declareFee',
                    },
                    {
                      name: 'trailerFee',
                      icon: 'smile',
                      path: '/item/itemList/trailerFee',
                      component: './item/itemList/trailerFee',
                    },
                    {
                      name: 'supplierFare',
                      icon: 'smile',
                      path: '/item/itemList/supplierFare',
                      component: './item/itemList/supplierFare',
                    },
                  ],
                },
              ],
            },
            {
              path: '/operate',
              name: 'operate',
              icon: 'smile',
              routes: [
                {
                  path: '/operate/tradeflow',
                  name: 'tradeflow',
                  icon: 'smile',
                  component: './operate/tradeflow',
                },
                {
                  path: '/operate/todoServiceOrder',
                  name: 'todoServiceOrder',
                  icon: 'smile',
                  component: './operate/todoServiceOrder',
                },
                {
                  path: '/operate/historyServiceOrder',
                  name: 'historyServiceOrder',
                  icon: 'smile',
                  component: './operate/historyServiceOrder',
                },
              ],
            },
            {
              path: '/workorder',
              name: 'workorder',
              icon: 'smile',
              routes: [
                {
                  path: '/workorder/logistics',
                  name: 'logistics',
                  icon: 'smile',
                  component: './workorder/logistics',
                },
                {
                  path: '/workorder/trailer',
                  name: 'trailer',
                  icon: 'smile',
                  component: './workorder/trailer',
                },
                {
                  path: '/workorder/customs',
                  name: 'customs',
                  icon: 'smile',
                  component: './workorder/customs',
                },
                {
                  path: '/workorder/back',
                  name: 'back',
                  icon: 'smile',
                  component: './workorder/back',
                },
                {
                  path: '/workorder/insurance',
                  name: 'insurance',
                  icon: 'smile',
                  component: './workorder/insurance',
                },
                {
                  path: '/workorder/valueAdded',
                  name: 'valueAdded',
                  icon: 'smile',
                  component: './workorder/valueAdded',
                },
              ],
            },
            {
              path: '/cost',
              name: 'cost',
              icon: 'smile',
              routes: [
                // {
                //   path: '/cost/record',
                //   name: 'record',
                //   icon: 'smile',
                //   component: './cost/record',
                // },
                {
                  path: '/cost/receivable',
                  name: 'receivable',
                  icon: 'smile',
                  component: './cost/receivable',
                },
                {
                  path: '/cost/payable',
                  name: 'payable',
                  icon: 'smile',
                  component: './cost/payable',
                },
              ],
            },
            {
              path: '/statistics',
              name: 'statistics',
              icon: 'smile',
              component: './Welcome',
              routes: [
                {
                  path: '/statistics/tradeMarket',
                  name: 'tradeMarket',
                  icon: 'smile',
                  component: './Welcome',
                },
              ],
            },
            {
              path: '/baseinfo',
              name: 'baseinfo',
              icon: 'smile',
              routes: [
                {
                  path: '/baseinfo/exchangeRate',
                  name: 'exchangeRate',
                  icon: 'smile',
                  component: './baseinfo/exchangeRate',
                },
                {
                  path: '/baseinfo/shippingLanes',
                  name: 'shippingLanes',
                  icon: 'smile',
                  component: './baseinfo/shippingLanes',
                },
                {
                  path: '/baseinfo/shippingCompany',
                  name: 'shippingCompany',
                  icon: 'smile',
                  component: './baseinfo/shippingCompany',
                },
                {
                  path: '/baseinfo/port',
                  name: 'port',
                  icon: 'smile',
                  component: './baseinfo/port',
                },
                {
                  path: '/baseinfo/costItems',
                  name: 'costItems',
                  icon: 'smile',
                  component: './baseinfo/costItems',
                },
                {
                  path: '/baseinfo/costTemplate',
                  name: 'costTemplate',
                  icon: 'smile',
                  component: './baseinfo/costTemplate',
                },
              ],
            },
            {
              path: '/system',
              name: 'system',
              icon: 'smile',
              routes: [
                {
                  path: '/system/user',
                  name: 'user',
                  icon: 'smile',
                  component: './system/user',
                },
                {
                  path: '/system/role',
                  name: 'role',
                  icon: 'smile',
                  component: './system/role',
                },
                {
                  path: '/system/resource',
                  name: 'resource',
                  icon: 'smile',
                  component: './system/resource',
                },
                {
                  path: '/system/rights',
                  name: 'rights',
                  icon: 'smile',
                  component: './system/rights',
                },
                {
                  path: '/system/lessee',
                  name: 'lessee',
                  icon: 'smile',
                  component: './system/lessee',
                },
                {
                  path: '/system/app',
                  name: 'app',
                  icon: 'smile',
                  component: './system/app',
                },
                {
                  path: '/system/namespace',
                  name: 'namespace',
                  icon: 'smile',
                  component: './system/namespace',
                },
              ],
            },
            {
              path: '/example',
              name: 'example',
              icon: 'bulb',
              component: './example/StandardModuleExample',
            },
            {
              component: './404',
            },
          ],
        },
        {
          component: './404',
        },
      ],
    },

    {
      component: './404',
    },
  ],
  // Theme for antd: https://ant.design/docs/react/customize-theme-cn
  theme: {
    'primary-color': primaryColor,
  },
  define: {
    ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION:
      ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION || '', // preview.pro.ant.design only do not use in your production ; preview.pro.ant.design 专用环境变量，请不要在你的项目中使用它。

    //本地环境
    MGMT_PATH: 'http://127.0.0.1:8087',
  },
  ignoreMomentLocale: true,
  lessLoaderOptions: {
    javascriptEnabled: true,
  },
  disableRedirectHoist: true,
  cssLoaderOptions: {
    modules: true,
    getLocalIdent: (
      context: {
        resourcePath: string;
      },
      _: string,
      localName: string,
    ) => {
      if (
        context.resourcePath.includes('node_modules') ||
        context.resourcePath.includes('ant.design.pro.less') ||
        context.resourcePath.includes('global.less')
      ) {
        return localName;
      }

      const match = context.resourcePath.match(/src(.*)/);

      if (match && match[1]) {
        const antdProPath = match[1].replace('.less', '');
        const arr = slash(antdProPath)
          .split('/')
          .map((a: string) => a.replace(/([A-Z])/g, '-$1'))
          .map((a: string) => a.toLowerCase());
        return `antd-pro${arr.join('-')}-${localName}`.replace(/--/g, '-');
      }

      return localName;
    },
  },
  manifest: {
    basePath: '/',
  },
  chainWebpack: webpackPlugin,
  /*
  proxy: {
    '/server/api/': {
      target: 'https://preview.pro.ant.design/',
      changeOrigin: true,
      pathRewrite: { '^/server': '' },
    },
  },
  */
} as IConfig;
